import { request } from 'umi'
import notify from "devextreme/ui/notify";
import getApiData from '@/services/cpasRequest';
import { message } from 'antd';
import { getExtension, getFileName } from '@/utils';




export async function deletePaper(data: any[], dbname: string, sjxmbh: any, zcbfid: string, userCode: string, userName: string): Promise<any> {
    console.log(data, 'data 接口data-----');

    const sjdgids: any[] = []
    const dglxs: any[] = []
    for (let i = 0; i < data.length; i++) {
        sjdgids.push(data[i].dgid)
        dglxs.push(data[i].dglx)
    }
    const res = await request('/cpasApi/draftManage/removeDrafts', {
        method: 'POST',
        data: {
            dbname: dbname,
            sjdgIds: sjdgids,
            dglxs: dglxs,
            sjxmbh: sjxmbh,
            zcbfId: zcbfid,
            userCode,
            userName
        }
    })
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}

export async function restorePaper(data: any[], dbname: string, zcbfid: string, sjxmbh: any, userCode: string, userName: string): Promise<any> {
    const sjdgids: any[] = []
    const dglxs: any[] = []
    for (let i = 0; i < data.length; i++) {
        sjdgids.push(data[i].dgid)
        dglxs.push(data[i].dglx)
    }
    const res = await request('/cpasApi/draftManage/restoreDraft', {
        method: 'POST',
        data: {
            sjdgIds: sjdgids,
            dglxs: dglxs,
            sjxmbh: sjxmbh,
            zcbfId: zcbfid,
            dbname: dbname,
            userCode,
            userName
        }
    })

    // if (res.ok) {
    //     notify('还原成功', 'success', 3000)
    //     return true
    // }
    // else {
    //     return false
    // }
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}

export async function clearRecycle(data: any[], dbname: string, sjxmbh: any, zcbfid: string): Promise<any> {
    if (data === undefined || data.length === 0) {
        notify('请选择要清空的文件', 'error', 3000)
        return false
    }
    const sjdgids: any[] = []
    const dglxs: any[] = []
    for (let i = 0; i < data.length; i++) {
        sjdgids.push(data[i].dgid)
        dglxs.push(data[i].dglx)
    }
    const res = await request('/cpasApi/draftManage/clearRecycle', {
        method: 'POST',
        data: {
            dbname: dbname,
            sjdgIds: sjdgids,
            dglxs: dglxs,
            sjxmbh: sjxmbh,
            zcbfId: zcbfid
        }
    })
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}


export async function getPaperProps(dbname: string, zcbfid: string, dgid: string, sjxmbh: string | undefined): Promise<any> {
    const res = await request('/cpasApi/draftManage/getFileProperties', {
        method: 'GET',
        params: {
            dgid: dgid,
            distPath: `${sjxmbh}/${zcbfid}`,
            dbname: dbname
        }
    })
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}



// // export async function deleteConfirm(data: any[], dbname:string, zcbfid:string) : Promise<any> {

// // }

// export default DeleteDialog;
// export default { deletePaper };

// 外部文件替换
export async function getPhaseTreeBatchReplace(
    user: any,
    dgids: any,
    sjxmbh: any,
    zcbfid: any,
    externalFilePath: any,
    dbname: any,
): Promise<any> {
    const res = await request('/cpasApi/draftManage/replaceByExternal', {
        method: 'POST',
        data: {
            externalFilePath: externalFilePath,
            dgids: dgids,
            user: user,
            zcbfid: zcbfid,
            sjxmbh: sjxmbh,
            dbname: dbname
        }
    });
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}

// 底稿平台重命名
export async function PaperRenname(dbname: string, zcbfid: string, sjxmbh: any, dgid: string, rename: any, dgsyh: string): Promise<any> {
    const newRename = getFileName(rename);
    const newExtension = getExtension(rename);
    const res = await request('/cpasApi/draftManage/dgidRename', {
        method: 'POST',
        data: {
            dgid: dgid,
            dbname: dbname,
            sjxmbh: sjxmbh,
            zcbfid: zcbfid,
            rename: newRename,
            redglx: newExtension,
            dgsyh: dgsyh
        }
    })
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}

// 新建文件夹
export async function newFile(dgid: string, sjxmbh: any, user: any, LinkDGID: any): Promise<any> {
    const res = await request('/cpasApi/draftFjManage/mountFolderToDg', {
        method: 'POST',
        data: {
            dgid: dgid,
            sjxmbh: sjxmbh,
            user: user,
            LinkDGID: LinkDGID,
            dbname: sjxmbh,
        }
    })
    return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: res.ok,
    });
}

export async function getTplList() {
    const r = await getApiData('pub.mbTypeList', {});

    return Promise.resolve({
        data: r.data,
        success: r.ok,
    });
}



